//
//  PopoverPresentationController.swift
//  vveboSwiftPro
//
//  Created by 杨笑怡 on 15/6/30.
//  Copyright © 2015年 YangXiaoYi. All rights reserved.
//

import UIKit

/// iOS 8.0 推出， 专门用于负责视图控制器之间的转场
class PopoverPresentationController: UIPresentationController {
    /// 展现视图的 frame
    var presentFrame = CGRectZero
    
    /// 遮罩视图
    lazy var dummyView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor.clearColor()
        
        let tap = UITapGestureRecognizer(target: self, action: "close")
        v.addGestureRecognizer(tap)
        
        return v
    }()
    
    /**
        containerView 容器视图，放置展现出来的视图
        presentedView 被展现出来的视图
    */
    ///  实例化转场控制器
    ///
    ///  :param: presentedViewController  被展现的控制器
    ///  :param: presentingViewController 文档上说，发起的控制器。在xCode6 nil，xCode7 野指针
    ///
    ///  :returns: 展现控制器
//    override init(presentedViewController: UIViewController, presentingViewController: UIViewController) {
//        
//        super.init(presentedViewController: presentedViewController, presentingViewController: presentedViewController)
//        
//        print(presentedViewController)
//        print(containerView)
//        
//    }
    
    ///  关闭展现视图控制器
    func close() {
        presentedViewController.dismissViewControllerAnimated(true, completion: nil)
    }
    
    ///  容器视图将要重新布局子视图
    override func containerViewWillLayoutSubviews() {
         super.containerViewWillLayoutSubviews()
        
        // 添加并设置dummyView
        dummyView.frame = containerView!.bounds
        containerView?.insertSubview(dummyView, atIndex: 0)
        
        // 设置视图大小
        presentedView()?.frame = presentFrame
    }

}
